require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'datetimepicker': '/js/lib/bootstrap-datetimepicker.min',
        'select2': '/js/lib/select2.min'
    },
    shim: {
        'bootstrap': ['jquery']
    }
});

require(['jquery', 'doT', 'common', 'bootstrap'], function($, doT, common) {
    $(function() {
        var provinceSelect = $('#provinceSelect');
        var citySelect = $('#citySelect');
        var districtSelect = $('#districtSelect');
        var provinces, citys, districts;

        $.getJSON(common.urlBase + '/location/province.json', function(result) {
            provinces = result.data;
            provinceSelect.html('');
            for (var i = 0; i < provinces.length; i++) {
                $('<option/>', { value: provinces[i].id }).html(provinces[i].name).appendTo(provinceSelect);
            }
            refreshCitySelect();
        });


        function refreshCitySelect() {
            citys = null;
            $.getJSON(common.urlBase + '/location/province-city/' + provinceSelect.val() + '.json', function(result) {
                citys = result.data;
                citySelect.html('');
                if (citys) {
                    for (var i = 0; i < citys.length; i++) {
                        $('<option/>', { value: citys[i].id }).html(citys[i].name).appendTo(citySelect);
                    }
                    refreshDistrictSelect();
                }
            });
        }

        function refreshDistrictSelect() {
            districts = null;
            $.getJSON(common.urlBase + '/location/city-area/' + citySelect.val() + '.json', function(result) {
                districts = result.data;
                districtSelect.html('');
                if (districts) {
                    for (var i = 0; i < districts.length; i++) {
                        $('<option/>', { value: districts[i].id }).html(districts[i].name).appendTo(districtSelect);
                    }
                }
            });
        }

        provinceSelect.on('change', function(e) {
            refreshCitySelect();
        });
        citySelect.on('change', function(e) {
            refreshDistrictSelect();
        });

        $('#addShop').on('click', function(e) {
            var error = false;
            var form = $('#addShopForm');
            var input = form.find(':input');
            input.each(function() {
                if (!$(this).val()) {
                    $('#noticeMsg').html('请输入正确的信息！');
                    $('#noticeModal').modal('show');
                    error = true;
                }
            });
            if (error === false) {
                var schoolData = $('#addShopForm').serialize();
                $.post(common.urlBase + '/school/addSchool/add.json', schoolData, function(result) {
                    if (result.ok === true) {
                        //添加学校成功
                    }
                    if (result.ok === false) {
                        //添加学校失败
                        $('#noticeMsg').html(result.message);
                        $('#noticeModal').modal('show');
                    }
                }, 'json');
            }
        });
    });
});
